<?php
/**
 * Created by PhpStorm.
 * User: fengliu
 * Date: 15/8/5
 * Time: 下午3:34
 */

namespace Admin\Controller;

class MediaController extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->meta_title = '视频管理';
    }

    public function index()
    {
        $title = I('get.title', '', 'op_t');
        $page = I('get.page','1','intval');
        $perPage = C('LIST_ROWS') < 20 ? 20 : C('LIST_ROWS');
        if (!empty($title))
            $where = array('title' => array('like', '%' . $title . '%'));
        $model = M('Video');
        $count = $model->where($where)->count();
        $maxPage=$count/$perPage;
        if($page!=1 && $page>$maxPage)
            $this->error('没有此页面');
        $Page = new \Think\Page($count, $perPage);
        $list = $model->where($where)->page($page, $perPage)->select();
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->show();
    }

    public function add()
    {
        if (IS_POST) {
            $remark = I('post.remark', '', 'op_t');
            $title = I('post.title', '', 'op_t');
            $swf = I('post.swf', '', 'op_t');
            if (empty($remark) && empty($title) && empty($swf)) {
                $this->error('各项内容不允许为空');
            }

            $data['remark'] = $remark;
            $data['title'] = $title;
            $data['swf'] = $swf;
            $result = M('Video')->add($data);
            if ($result)
                $this->success('保存成功,id为' . $result, U('index'));
            else
                $this->error(M('Video')->getError());
        } else {
            $this->display('edit');
        }
    }

    public function edit()
    {
        $id = I('get.id', 0, 'intval');
        if(!$id)
            $id=I('post.id',0,'intval');
        if (empty($id))
            $this->redirect('index');
        $data = M('Video')->find($id);
        if(empty($data)){
            $this->redirect('index');
        }elseif (IS_POST) {
            $remark = I('post.remark', '', 'op_t');
            $title = I('post.title', '', 'op_t');
            $swf = I('post.swf', '', 'op_t');
            if (empty($remark) && empty($title) && empty($swf)) {
                $this->error('各项内容不允许为空');
            }
            $data['remark'] = $remark;
            $data['title'] = $title;
            $data['swf'] = $swf;
            $result = M('Video')->save($data);
            if ($result>=0)
                $this->success('受影响行数为' . $result, U('index'));
            else
                $this->error(M('Video')->getError());
        } else {

            $this->assign('info', $data);
            $this->display();
        }
    }
    public function del(){
        $id=I('get.id',0,'intval');
        M('Video')->delete($id);
        $this->success('删除成功',U('index'));
    }
}